﻿using Autofac.Core;

using Org.BouncyCastle.Utilities;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PLCS.PlcInteraction
{
    public class ErrorCode
    {
        /// <summary>
        /// 将PLC报警代码转为报警集合
        /// </summary>
        /// <param name="errorCode">报警代码</param>
        /// <param name="alarmDeviceType">报警设备类型</param>
        /// <param name="index">代码集合序号（第几个DINT）</param>
        /// <returns></returns>
        public List<string> Parse(int errorCode, AlarmDeviceType alarmDeviceType, int index)
        {
            var result = new List<string>();
            string binaryString = Convert.ToString(errorCode, 2).PadLeft(16, '0');
            var errorCodes = binaryString.ToArray();
            Array.Reverse(errorCodes);
            for (int i = 0; i < errorCodes.Length; i++)
            {
                if (errorCodes[i] == '1')
                {
                    result.Add(GetErrorString(i + index * 16, alarmDeviceType));
                }
            }
            return result;
        }

        private string GetErrorString(int number, AlarmDeviceType alarmDeviceType)
        {
            return "测试";
        }
    }

    public enum AlarmDeviceType
    {
        RGV,
        Stacker,
        ConveyorLine,
        AGV,
        RotaryTable,
    }
}